<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/js/layui-v2.4.5/layui/css/layui.css}" media="all" />
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all" />
    <link rel="stylesheet" th:href="@{/js/public/dtree/dtree.css}" media="all" />
    <link rel="stylesheet" th:href="@{/js/public/dtree/font/dtreefont.css}" media="all" />
</head>
<body  style="-webkit-backface-visibility: hidden;">

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>新增用户</legend>
</fieldset>

<form class="layui-form"  lay-filter="example">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入用户名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="password" lay-verify="title" autocomplete="off" placeholder="请输入密码" class="layui-input" value="123456">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" placeholder="请输入" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">所属角色</label>
        <div class="layui-input-block" id="roles" name ="roles" lay-filter="roles">

        </div>

    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">所属权限</label>
        <div class="layui-input-block" id="privs" name ="privs" lay-filter="privs">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单权限</label>
        <div class="layui-input-block">
            <ul id="demoTree" class="dtree" data-id="0" style="border:1px solid #000"></ul>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block" style="text-align: center">
            <button type="button" class="layui-btn layui-btn-normal" id ="back">返回</button>
            <button   class="layui-btn" lay-submit lay-filter="save">保存</button>
        </div>
    </div>
</form>


<script th:src="@{/js/jquery-2.1.1.min.js}"></script>
<script th:src="@{/js/layui-v2.4.5/layui/layui.js}"></script>
<script th:src="@{/js/utils.js}"></script>
<script th:inline="javascript">
    var username = [[${username}]];
</script>
<script>
    layui.config({
        base: path+'/js/public/'
    }).extend({
        selectPlus: 'selectPlus/selectPlus',
        dtree: 'dtree/dtree',
        selectM: 'selectN/selectM'
    });
    layui.use(['form','selectPlus','laydate','dtree','selectM'], function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : parent.layer;
             $ = layui.jquery,
                 selectPlus = layui.selectPlus,
                 selectM = layui.selectM,
              dtree = layui.dtree, laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#birthday'
            ,type: 'date'
            ,format:"yyyy-MM-dd"
        });

        $("#back").click(function () {
            var index=layer.getFrameIndex(window.name); //获取当前窗口的name
            layer.close(index);
        })

        var privs,roles;
        //加载所有角色列表，供选择
        $.ajax({
            type: "POST",
            url:  path+'/role/getAllRole',
            dataType:"json",
            data:{},
            success: function (data) {
                //多选标签-所有配置
                roles = selectM({
                    elem: '#roles'    //元素容器【必填】
                    ,data: data     //候选数据【必填】
                    ,selected: []   //默认值
                    ,max : 12  //最多选中个数，默认5
                    ,name: 'roleName'     //input的name 不设置与选择器相同(去#.)
                    ,delimiter: ','        //值的分隔符
                    ,field: {idName:'id',titleName:'roleName'}            //候选项数据的键名
                });
            },
            error: function (msg) {
            }
        });

        /*
         加载所有权限列表，供选择
         */
        $.ajax({
            type: "POST",
            url:  path+'/priv/getAllPriv',
            dataType:"json",
            data:{},
            success: function (data) {
                privs = selectPlus.render({
                    el: '#privs',
                    data:data,
                    label:["privName"],
                    valueName: "privName", //获取哪一个值填充
                    values: [],//默认选中的值
                    valueSeparator: "，"
                });

            },
            error: function (msg) {
            }
        });


        //加载所有菜单列表供选择
        /**
         * 初始化菜单树
         * */
        var DemoTree = dtree.render({
            elem: "#demoTree",
            type: "all",
            initLevel: "5",
            url:path+"/priv/getAllMenu",
            checkbar: true,
            checkbarType: "all",
            response: {statusCode: 0, rootName: "dataTree", treeId:"treeId"} // 这里指定了返回的数据格式，组件会根据这些值来替换返回JSON中的指定格式，从而读取信息
        });


        /**
         *
         *监听提交
         * */
        form.on('submit(save)', function(data){
            var load = layer.load();
            var data = data.field;

            var obj = privs.getChecked();
            var priv = obj.data;

            console.log("权限："+priv);
            $.each(priv, function(i) {
                data['privIds['+i+']']=priv[i].id
            });
            var role = roles.selected;

            console.log("角色："+priv);
            if(role.length==0){
                layer.msg("请选择角色",{icon: 5})
                return false ;
            }
            $.each(role, function(i) {
                data['roleIds['+i+']']=role[i].id
            });


            var params = dtree.getCheckbarNodesParam("demoTree");
//            if(params.length<=0){
//                layer.msg("请选择菜单权限",{icon: 5})
//                return ;
//            }
            $.each(params, function(i) {
                data['resourceIds['+i+']']=params[i].nodeId
            });

            console.log("提交数据:"+data);
            $.ajax({
                type: "POST",
                url:  path+'/users/saveUser',
                dataType:"json",
                data:data,
                success: function (data) {
                    layer.close(load);
                    if(data.code=="1"){
                        layer.msg(data.msg,{icon: 6})
                        window.parent.hasSave=true;
                        var index=layer.getFrameIndex(window.name); //获取当前窗口的name
                        layer.close(index);
                    }else{
                        layer.msg(data.msg,{icon: 5})
                    }
                },
                error: function (msg) {
                    layer.close(load);
                }
            });
            return false;
        });

    });

</script>

</body>
</html>